home *** CD-ROM | disk | FTP | other *** search
- # Source Generated with Decompyle++
- # File: in.pyc (Python 2.6)
-
- import os
- import rhythmdb
- import rb
- import gobject
- import gio
- IMAGE_NAMES = [
- 'cover',
- 'album',
- 'albumart',
- '.folder',
- 'folder']
- ITEMS_PER_NOTIFICATION = 10
- ART_SAVE_NAME = 'Cover.jpg'
- ART_SAVE_FORMAT = 'jpeg'
- ART_SAVE_SETTINGS = {
- 'quality': '100' }
-
- def file_root(f_name):
- return os.path.splitext(f_name)[0].lower()
-
-
- def shared_prefix_length(a, b):
- l = 0
- while a[l] == b[l]:
- l = l + 1
- return l
-
-
- class LocalCoverArtSearch:
-
- def __init__(self):
- pass
-
-
- def _enum_dir_cb(self, fileenum, result, .3):
- (results, on_search_completed_cb, entry, args) = .3
-
- try:
- files = fileenum.next_files_finish(result)
- if files is None or len(files) == 0:
- print 'okay, done; got %d files' % len(results)
- on_search_completed_cb(self, entry, results, *args)
- return None
- for f in files:
- ct = f.get_attribute_string('standard::fast-content-type')
- if ct.startswith('image/') and f.get_attribute_boolean('access::can-read'):
- results.append(f.get_name())
- continue
- len(files) == 0
-
- fileenum.next_files_async(ITEMS_PER_NOTIFICATION, callback = self._enum_dir_cb, user_data = (results, on_search_completed_cb, entry, args))
- except Exception:
- e = None
- print 'okay, probably done: %s' % e
- on_search_completed_cb(self, entry, results, *args)
-
-
-
- def search(self, db, entry, on_search_completed_cb, *args):
- self.file = gio.File(entry.get_playback_uri())
- if self.file.get_uri_scheme() in ('http', 'cdda'):
- print 'not searching for local art for %s' % self.file.get_uri()
- on_search_completed_cb(self, entry, [], *args)
- return None
- self.artist = db.entry_get(entry, rhythmdb.PROP_ARTIST)
- self.album = db.entry_get(entry, rhythmdb.PROP_ALBUM)
- print 'searching for local art for %s' % self.file.get_uri()
- parent = self.file.get_parent()
- enumfiles = parent.enumerate_children(attributes = 'standard::fast-content-type,access::can-read,standard::name')
- enumfiles.next_files_async(ITEMS_PER_NOTIFICATION, callback = self._enum_dir_cb, user_data = ([], on_search_completed_cb, entry, args))
-
-
- def search_next(self):
- return False
-
-
- def get_best_match_urls(self, results):
- parent = self.file.get_parent()
- for name in [
- file_root(self.file.get_basename())] + IMAGE_NAMES:
- for f_name in results:
- if file_root(f_name) == name:
- yield parent.resolve_relative_path(f_name).get_uri()
- continue
-
-
- artist = self.artist.lower()
- album = self.album.lower()
- for f_name in results:
- f_root = file_root(f_name).lower()
- if f_root.find(artist) != -1 and f_root.find(album) != -1:
- yield parent.resolve_relative_path(f_name).get_uri()
- continue
-
- match = (2, None)
- for f_name in results:
- pl = shared_prefix_length(f_name, self.file.get_basename())
- if pl > match[0]:
- match = (pl, f_name)
- continue
-
- if match[1] is not None:
- yield parent.resolve_relative_path(match[1]).get_uri()
-
-
-
- def pixbuf_save(self, plexer, pixbuf, uri):
-
- def pixbuf_cb(buf):
- f = gio.File(uri)
- f.replace_contents_async(buf, plexer.send())
- yield None
- (file, result) = (_,)
-
- try:
- file.replace_contents_finish(result)
- except Exception:
- plexer.receive()
- e = plexer.receive()
- print 'error creating "%s": %s' % (uri, e)
- except:
- plexer.receive()
-
-
- pixbuf.save_to_callback(pixbuf_cb, ART_SAVE_FORMAT, ART_SAVE_SETTINGS)
-
-
- def _save_dir_cb(self, enum, result, .3):
- (db, entry, dir, pixbuf) = .3
- (artist, album) = [ db.entry_get(entry, x) for x in [
- rhythmdb.PROP_ARTIST,
- rhythmdb.PROP_ALBUM] ]
-
- try:
- files = enum.next_files_finish(result)
- for f in files:
- ct = f.get_attribute_string('standard::fast-content-type')
- if ct.startswith('image/') or ct.startswith('x-directory/'):
- continue
-
- uri = dir.resolve_relative_path(f.get_name())
- u_entry = db.entry_lookup_by_location(uri)
- print 'Not saving local art; encountered unknown file (%s)' % uri
- enum.close()
- return None
- except Exception:
- []
- e = []
- []
- print 'Error reading "%s": %s' % (dir, exception)
- except:
- []
-
-
-
- def save_pixbuf(self, db, entry, pixbuf):
- uri = entry.get_playback_uri()
- if uri is None or uri == '':
- return None
- f = gio.File(uri)
- if uri.get_uri_scheme() == 'http':
- print 'not saving local art for %s' % uri
- return None
- print 'checking whether to save local art for %s' % uri
- parent = f.get_parent()
- enumfiles = parent.enumerate_children(attributes = 'standard::fast-content-type,access::can-read,standard::name')
- enumfiles.next_files_async(ITEMS_PER_NOTIFICATION, callback = self._save_dir_cb, user_data = (db, entry, parent, pixbuf))
-
-
-